package com.example.meterialproject.activitys.transition.demo1

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.example.meterialproject.R
import com.example.meterialproject.exts.createRecyclerView
import com.google.android.material.transition.MaterialContainerTransform
import java.util.concurrent.TimeUnit

/**
 *
 * @ClassName: TransitionDemo1Fragment
 * @Author: android 超级兵
 * @CreateDate: 4/26/22$ 2:21 PM$
 * TODO
 */
class TransitionDemoFrom1Fragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        // 返回
        postponeEnterTransition(0, TimeUnit.MILLISECONDS)
        return inflater.inflate(R.layout.layout_recycler_view, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.findViewById<RecyclerView>(R.id.recycler_view)
            .createRecyclerView(requireContext()) { itemView, url, _ ->

                // 创建 fragment
                val fragment = TransitionDemoTo1Fragment.newInstance(url)
                val transform = MaterialContainerTransform(requireContext(), true)
                transform.fadeMode = MaterialContainerTransform.FADE_MODE_OUT
                fragment.sharedElementEnterTransition = transform


                parentFragmentManager
                    .beginTransaction()
                    // 设置共享元素
                    .addSharedElement(itemView, ViewCompat.getTransitionName(itemView) ?: "")
                    .setCustomAnimations(
                        R.anim.enter_in,
                        R.anim.exit_in,
                        R.anim.enter_out,
                        R.anim.exit_out,
                    )
                    .replace(R.id.frameLayout, fragment, TransitionDemoTo1Fragment.TAG)
                    // 回退tag
                    .addToBackStack(TransitionDemoTo1Fragment.TAG)
                    .commit()
            }
    }


}